#include "SectorBond.h"

#include <core/strings/string_util.h>

#define SECTOR_GREEN "1002306"
#define SECTOR_SOCIETY "1002307"
#define SECTOR_SUSTAINABLE "1002308"
#define SECTOR_SMALL_CORP "1002305"
#define SECTOR_INOVATION_CORP "1002304"
#define SECTOR_PANDA "1000113"

SectorBond& SectorBond::instance() {
  static SectorBond s_pb;
  return s_pb;
}

SectorBond::SectorBond() {
  m_sectorVector = std::vector<std::string>{
      SECTOR_GREEN,      SECTOR_SOCIETY,        SECTOR_SUSTAINABLE,
      SECTOR_SMALL_CORP, SECTOR_INOVATION_CORP, SECTOR_PANDA};
}

SectorBond::~SectorBond() {}

void SectorBond::ClearData() {
  std::lock_guard<std::mutex> locker(m_mutex);
  m_data.clear();
}

bool SectorBond::IsInSector(int nIndex, const char* comBondCode) {
  if (nIndex < 0 || nIndex >= (int)m_sectorVector.size()) return false;

  auto it = m_data.find(m_sectorVector[nIndex]);
  if (it == m_data.end()) return false;

  return it->second.find(comBondCode) != it->second.end();
}

void SectorBond::UpdateData(SectorBondAck* pData) {
  if (!pData) return;
  std::lock_guard<std::mutex> locker(m_mutex);

  for (auto it : pData->m_bondList) {
    if (it.m_comBondCode[0] == '\0') continue;
    std::vector<qb::base::BaseString> strings;

    qb::base::StringUtil::SplitString(it.m_sectorCode,
                                      _countof(it.m_sectorCode), "|", strings);
    for (auto itSector : strings) {
      m_data[itSector.c_str()].insert(it.m_comBondCode);
    }
  }
}
